package com.unitechs.trap.excel.webapi.service.impl;

import com.unitechs.trap.common.pojo.trap.entity.AllData;
import com.unitechs.trap.common.pojo.trap.entity.TrapType;
import com.unitechs.trap.common.pojo.trap.entity.VarData;
import com.unitechs.trap.excel.service.IAllDataService;
import com.unitechs.trap.excel.service.ICreatAlarmCodeService;
import com.unitechs.trap.excel.service.IReadAllDataService;
import com.unitechs.trap.excel.service.IReadVarDataService;
import com.unitechs.trap.excel.webapi.mapper.AllDataMapper;
import com.unitechs.trap.excel.webapi.mapper.ReadAlaOutMapper;
import com.unitechs.trap.excel.webapi.mapper.VarDataMapper;
import com.unitechs.trap.excel.webapi.utils.StreamUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@DubboService
@Slf4j
@Service
public class AllDataServiceImpl implements IAllDataService {
    @Autowired
    AllDataMapper allDataMapper;
    @Autowired
    VarDataMapper varDataMapper;
    @Autowired
    ReadAlaOutMapper readAlaOutMapper;

    @Autowired
    private IReadAllDataService readAllDataFromExcel;
    @Autowired
    private IReadVarDataService readVarDataFromExcel;
    @Autowired
    private ICreatAlarmCodeService creatAlarmCodeService;

    /**
     * 清除alldata
     */
    @Override
    public void clearAllData() {
        allDataMapper.truncateAllData();
    }

    /**
     * alldata入库
     * 数据入库allData
     * @param url
     * @param coName
     */
    @Override
    public void insertAllData(String url, String coName, String version) {
        List<AllData> allData = getAllData(url, coName, version);
        allDataMapper.insertAllData(allData);
    }

    /**
     * 清空vardata
     */
    @Override
    public void clearVarData() {
        varDataMapper.truncateVarData();
    }

    /**
     * vardata入库
     * 数据入库varData
     * @param url
     * @param coName
     */
    @Override
    public void insertVarData(String url, String coName, String version) {
        List<VarData> varData = getVarData(url, coName, version);

        if (varData == null || varData.isEmpty()) {
            return;
        }
        varDataMapper.insertVarData(varData);
    }


    /**
     * 获取alldata数据用于发送给入库方法
     *
     * @param url
     * @param coName
     * @return
     */
    private List<AllData> getAllData(String url, String coName, String version) {

        List<AllData> list = readAllDataFromExcel.readAllDataByVersion(url, coName, version);//从excel读取数据并赋值部分属性
        list = deduplicationAllDataByOid(list);//去重
        list= deduplicationSecAllDataByDescr(list);//二次去重
        list = creatAlarmCodeService.CreatAlarmCode(list, url, coName, version);//生成code，并判断恢复还是告警

        return list;
    }

    /**
     * 获取vardata数据用于发送给入库方法
     *
     * @param url
     * @param coName
     * @return
     */
    private List<VarData> getVarData(String url, String coName, String version) {
        List<VarData> list = readVarDataFromExcel.readVarDataByVersion(url, coName, version);
        return list;
    }


    /**
     * 去掉重复数据
     * 根据OID去重
     *
     * @param allData
     * @return
     */
    private List<AllData> deduplicationAllDataByOid(List<AllData> allData) {

        Set<String> uniqueOids = new HashSet<>();
        List<AllData> filteredList = new ArrayList<>();
        for (AllData s : allData) {
            if (uniqueOids.add(s.getTrapOid())) {
                filteredList.add(s);

            }

        }
        return filteredList;
    }

    /**
     * 二次去重
     * 根据告警标题
     *
     * @param allData
     * @return
     */
    private List<AllData> deduplicationSecAllDataByDescr(List<AllData> allData) {
        List<TrapType> trapTypes = readAlaOutMapper.listTrapOidFromAlaOut();
        Set<String> uniqueOids = new HashSet<>();
        for (TrapType str:trapTypes){
            uniqueOids.add(str.getTrapOid());
        }
        List<AllData> filteredList = new ArrayList<>();
        for (AllData s : allData) {
            if (uniqueOids.add(s.getTrapOid())) {
                filteredList.add(s);
            }

        }
        return filteredList;
    }


}
